﻿using CommonClass;
using EnvCommonUtility;
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;


namespace EnvControlLib
{
    #region Object2VisibilityConverter

    /// <summary>
    /// Object 类型转 Visibility
    /// </summary>
    public class Object2VisibilityConverter : IValueConverter
    {
        /// <summary>
        /// Object -> Visibility
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
                return Visibility.Visible;
            else
                return Visibility.Collapsed;
        }

        /// <summary>
        /// Visibility -> Object
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

    #endregion

    #region BooleanConverter

    /// <summary>
    /// Boolean 类型转换组
    /// </summary>
    public class BooleanConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            foreach (object value in values)
            {
                if ((bool)value)
                {
                    return true;
                }
            }

            return false;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

    #endregion

    #region String2StringConverter

    /// <summary>
    /// String 类型转 String
    /// </summary>
    public class String2StringConverter : IValueConverter
    {
        /// <summary>
        /// String -> String
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, 
            System.Globalization.CultureInfo culture)
        {
            if (value != null && parameter is string)
            {
                switch (parameter as string)
                {
                    case "LeafStatus":
                        LeafStatusEnum outLeafStatus;
                        if (Enum.TryParse<LeafStatusEnum>(value.ToString(), true, out outLeafStatus))
                        {
                            return Utilities.GetEnumDisplayDescription(outLeafStatus);
                        }
                        break;
                    

                    default:
                        return value.ToString();
                }
            }

            if (value != null)
                return value.ToString();
            else
                return string.Empty;
        }

        /// <summary>
        /// String -> String
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

    #endregion
}
